<html>
  <script>
    aesCodec = null;

    function moduleDidLoad() {
        aesCodec = document.getElementById('aesCodec');
    }

    // Sends |decryptPage| request to the current page.
    // Called from popup page.
    function processUserPassword(pwd) {
      var msg = {type:"decryptPage", pwd:pwd};
        chrome.tabs.getSelected(null, function(tab) {
          chrome.tabs.sendRequest(tab.id, msg);
        });
    }

    // Handles messages from the current page:
    // showIcon - enables extension |unlock| icon in the browser UI
    // hideIcon - hides extension |unlock| icon in the browser UI
    // decrypt - decrypts cipher text into plaintext
    // http://code.google.com/chrome/extensions/messaging.html
    chrome.extension.onRequest.addListener(
      function(request, sender, sendResponse) {
        var response = {};
        if (request.type == "decrypt") {
          if (aesCodec && aesCodec.Decrypt) {
            //aesCodec.Decrypt() is implemented in crypto_<...>.nexe module.
            var plainText = aesCodec.Decrypt(request.pwd,request.cipherText);
            if (plainText)
              response = {type:"plainText",plainText:plainText}; 
          }
        } else if (request.type == "showIcon") {
          chrome.pageAction.show(sender.tab.id);
        } else if (request.type == "hideIcon") {
          chrome.pageAction.hide(sender.tab.id);
        }
        sendResponse(response); 
      });
  </script>
  <body>
    <embed name="aesCodec"
           id="aesCodec"
           width=0
           height=0
           type="application/x-nacl-srpc" 
           onload="moduleDidLoad()"/>
    <script type="text/javascript">
      var nexes = 'x86-32: crypto_x86_32.nexe\n'
                + 'x86-64: crypto_x86_64.nexe\n'
                + 'ARM: crypto_arm.nexe ';
      document.getElementById('aesCodec').nexes = nexes;
    </script>
  </body>
</html>

